package com.spring.myspace.web.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.spera.aliyunContent.AliyunContent;
import com.spera.aliyunUtil.CreateOSSClient;
import com.spring.myspace.web.dao.*;
import com.spring.myspace.web.model.FileNeedDelete;
import com.spring.myspace.web.service.MySpaceCommon;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
@Service
public class MySpaceCommonImpl implements MySpaceCommon{
	@Resource
	private MySpaceCommonMapper mySpaceCommonMapper;
	
    @Resource
    private JedisCache jedisCache;
    
    /**
     * 检验是否有每天相册 没有就生成一个
     */
    @Override  
public String checkJournal(String c_family_member_id) throws Exception{
		
    	int count=mySpaceCommonMapper.checkHasJournalAlbum(c_family_member_id);
    	
    	if(count>=1){
    	String[] journalAlbumId =	mySpaceCommonMapper.getJournalAlbumId(c_family_member_id);
    	return journalAlbumId[0];
    	}else{
    		String uuid=ApplicationUtils.randomUUID();
    		int effect= mySpaceCommonMapper.createJournalAlbum(c_family_member_id);
    		if(effect==1){
    			return uuid;
    		}else{
    			return null;
    		}
    	}

	}
    
    @Override  
    public int  checkIsNeedDelete(String file_id) throws Exception{
    	
    	System.out.println("checkIsNeedDelete");
    	
    	int count=mySpaceCommonMapper.checkIsNeedDelete(file_id);
    	if(count==0){
    		String key=mySpaceCommonMapper.getFileKeyById(file_id);
    		if(key!=null){
			CreateOSSClient.getInstance().getOSSClient().deleteObject(AliyunContent.inputBucket, key);
    		}
			int effect=mySpaceCommonMapper.deleteFileById(file_id);
			return 0;
    	}else{
    		return 1;
    	}
    	
    }
    
    @Override  
    public void  checkIsNeedDelete(List<FileNeedDelete> list) throws Exception{
    	
    	System.out.println(list.size());
    	
    	for(FileNeedDelete fnd:list){
    		
    		if(fnd!=null){
    			System.out.println("fnd不空");
    		
    		
    	int count=mySpaceCommonMapper.checkIsNeedDelete(fnd.getBase_oss_file_id());
    	if(count==0){
    		String key=mySpaceCommonMapper.getFileKeyById(fnd.getBase_oss_file_id());
    		if(key!=null){
			CreateOSSClient.getInstance().getOSSClient().deleteObject(AliyunContent.inputBucket, key);
    		}
			int effect=mySpaceCommonMapper.deleteFileById(fnd.getBase_oss_file_id());
			int effect2=mySpaceCommonMapper.deleteVidoeById(fnd.getBase_oss_file_id());
			
			//return 0;
    	
    	}else{
    		//return 1;
    	}
    		}
    }
    	
    }
}
